<?php
session_start();
/**
 * JS_API支付demo
 * ====================================================
 * 在微信浏览器里面打开H5网页中执行JS调起支付。接口输入输出数据格式为JSON。
 * 成功调起支付需要三个步骤：
 * 步骤1：网页授权获取用户openid
 * 步骤2：使用统一支付接口，获取prepay_id
 * 步骤3：使用jsapi调起支付
*/
	$appid = 'wx1a7fc6711244e4f4';
	$appsecret = '860f2c3adfcfb3743fe538f6281ddbfa';
	function _curl($url) {
		
		$ch = curl_init();
		
		curl_setopt($ch, CURLOPT_TIMEOUT, 30);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_HEADER, FALSE);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		$res = curl_exec($ch);
		curl_close($ch);
		$data = json_decode($res, true);
		return $data;
	}
	//step 1 get code
	
	// if (!isset($_GET['code']))
	// {
		// //触发微信返回code码
		// $retUrl =  'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		// $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$retUrl&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
		// Header("Location: $url"); 
	// } else {
		// $code = $_GET['code'];
		// $url ="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
		// $data = _curl($url);
		// //var_dump($data);
		// $accessToken = $data['access_token'];
// 		
		// $oldTimestamp = time();
		// $url =  'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		// // appId=wx17ef1eaef46752cb
		// // url=http://open.weixin.qq.com/
		// // timeStamp=1384841012
		// // nonceStr=123456
		// // accessToken
		// //OezXcEiiBSKSxW0eoylIeBFk1b8VbNtfWALJ5g6aMgZHaqZwK4euEskSn78Qd5pLsfQtuMdgmhajVM5QDm24W8X3tJ18kz5mhmkUcI3RoLm7qGgh1cEnCHejWQo8s5L3VvsFAdawhFxUuLmgh5FRA&appid=wx17ef1eaef46752cb&noncestr=123456&timestamp=1384841012&url=http://open.weixin.qq.com/
		// $bizString = "accesstoken=".$accessToken."&appid=".$appid."&noncestr=123456&timestamp=".$oldTimestamp."&url=".$url;
		// echo $bizString;
// 		
	// }
	$accessToken = $_SESSION['accessToken'];
	$oldTimestamp = time();
	$url =  'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
	$bizString = "accesstoken=".$accessToken."&appid=".$appid."&noncestr=123456&timestamp=".$oldTimestamp."&url=".$url;
	$addrSign = sha1($bizString);	
	
?>
<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    <title>微信安全支付</title>

	<script type="text/javascript">

		//调用微信JS api 支付
		function jsApiCall()
		{
			WeixinJSBridge.invoke(
				'editAddress',
				{
					 "appId": "<?php echo $appid;?>",
					 "scope": "jsapi_address",
					 "signType": "sha1",
					 "addrSign": "<?php echo $addrSign;?>",
					 "timeStamp": "<?php echo $oldTimestamp;?>",
					 "nonceStr": "123456",
				},
				function(res){
					WeixinJSBridge.log(res.err_msg);
					alert(res.err_code+'|'+res.err_desc+'|'+res.err_msg);
				}
			);
		}

		function callpay()
		{
			if (typeof WeixinJSBridge == "undefined"){
			    if( document.addEventListener ){
			        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
			    }else if (document.attachEvent){
			        document.attachEvent('WeixinJSBridgeReady', jsApiCall); 
			        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
			    }
			}else{
			    jsApiCall();
			}
		}
	</script>
</head>
<body>
	</br></br></br></br>
	<div align="center">
		<button style="width:210px; height:30px; background-color:#FE6714; border:0px #FE6714 solid; cursor: pointer;  color:white;  font-size:16px;" type="button" onclick="callpay()" >贡献一下</button>
	</div>
</body>
</html>